package org.zmhhxl.samples3.resource.gateway.advice;


//import jakarta.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zmhhxl.samples3.resource.gateway.utils.Result;


/**
 * 全局异常处理器
 *
 * @author zmhhxl
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler /*extends AbstractErrorWebExceptionHandler*/ {
//    public GlobalExceptionHandler(GlobalExceptionHandlerProperties properties,
//                                  ResourceProperties resourceProperties,
//                                  ApplicationContext applicationContext,
//                                  ServerCodecConfigurer serverCodecConfigurer) {
//        super(new DefaultErrorAttributes(), new GlobalExceptionHandlerProperties(), resourceProperties);
//        super.setMessageWriters(serverCodecConfigurer.getWriters());
//        super.setMessageReaders(serverCodecConfigurer.getReaders());
//    }
//
//    @Override
//    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
//        return RouterFunctions.route(RequestPredicates.all(), this::handleError);
//    }
//
//    private Mono<ServerResponse> handleError(ServerRequest request) {
//        Map<String, Object> errorAttributes = getErrorAttributes(request, false);
//        Throwable error = getError(request);
//        // 根据异常类型和信息，返回相应的错误响应
//        return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
//              .contentType(MediaType.APPLICATION_JSON)
//              .body(BodyInserters.fromValue(errorAttributes));
//    }

    /**
     * 404 异常处理
     *
     * @param response 响应对象
     * @param ex       具体的异常实例
     * @return 统一响应类
     */
//    @ExceptionHandler(WebClientResponseException.class)
//    public Result<String> exception(ServerHttpResponse response, RuntimeException ex) {
//        log.error(ex.getMessage(), ex);
//        response.setStatusCode(ex.getMessage());
//        return Result.error(ex.getStatusCode().value(), ex.getReason());
//    }

//        @ExceptionHandler(OAuth2AuthenticationException.class)
//    public Result<String> exception(ServerHttpResponse response, RuntimeException ex) {
//        log.error(ex.getMessage(), ex);
//        response.setStatusCode(HttpStatusCode.valueOf(404));
//        return Result.error(ex..value(), ex.getReason());
//    }

    /**
     * 503 服务不可用
     *
     * @param response 响应对象
     * @param ex       具体的异常实例
     * @return 统一响应类
     */
    @ExceptionHandler(NotFoundException.class)
    public Result<String> exception(ServerHttpResponse response, NotFoundException ex) {
        log.error(ex.getReason(), ex);
        response.setStatusCode(ex.getStatusCode());
        return Result.error(ex.getStatusCode().value(), ex.getReason());
    }
}
